package 双指针;

/**
 * https://leetcode.cn/problems/reverse-integer/
 */
public class _7_整数反转 {

    public static void main(String[] args) {
        int a = 123;
        _7_整数反转 _7_整数反转 = new _7_整数反转();
        int result = _7_整数反转.reverse(a);
        System.out.println(result);
    }

    public int reverse(int x) {
        int result = 0;
        while (x != 0) {
            int a = x % 10;

            // 防止溢出
            if(result > Integer.MAX_VALUE || result > Integer.MAX_VALUE /10) {
                return 0;
            }

            if(result < Integer.MIN_VALUE || result < Integer.MIN_VALUE /10) {
                return 0;
            }

            result = result * 10 + a;
            x = x/10;
        }

        return result;
    }
}
